#include <stdlib.h>
#include <string.h>
#include "Student.h"
#include <stdio.h>

void saveFile(char *fileName,struct studentNode* listHeadNode)
{
	FILE *fp;
	fp = fopen(fileName,"w+");
	struct studentNode* pmove = listHeadNode->next;
	while(pmove)
	{
		fprintf(fp,"%lld,%s,%s,%s\n",pmove->data.num,pmove->data.name,pmove->data.sex,pmove->data.major);
		pmove = pmove->next;
	}
	fclose(fp);	
}

void readFile(char *fileName,struct studentNode* listHeadNode)
{
	struct student tempData;
	FILE* fp;
	char temp[10010];
	char *token;
	fp = fopen(fileName,"r");
	if(fp == NULL){
		fp = fopen(fileName,"w+");
	}
	while(fscanf(fp,"%lld,%s\n",&tempData.num,&temp)!=EOF)
	{
		token = strtok(temp,",");
		
		strcpy(tempData.name,token);
		token = strtok(NULL,",");
		strcpy(tempData.sex,token);
		token = strtok(NULL,",");
		strcpy(tempData.major,token);
		
		while(token != NULL){
			token = strtok(NULL,",");	
		}
		
		struct student studentNode = tempData;
		struct student tempData;
		insertNodeByHead(listHeadNode,studentNode); 
	}
}

void readUser(char *fileName,struct user *tempData)
{

	
	system("attrib -h .Useraccount.csv");
	FILE* fp;
	char temp[450];
	char *token;
	fp = fopen(fileName,"r");
	if(fp == NULL){
		fp = fopen(fileName,"w+");
	}
	system("attrib +h .Useraccount.csv");
	fscanf(fp,"%s\n",&temp);
	token = strtok(temp,",");
	
	strcpy(tempData->account,token);
	token = strtok(NULL,",");
	strcpy(tempData->password,token);

	
	while(token != NULL){
		token = strtok(NULL,",");	
	}
		

}

void saveUser(char *fileName,struct user newUser)
{
	system("attrib -h .Useraccount.csv");
	FILE *fp;
	fp = fopen(fileName,"w+");
	fprintf(fp,"%s,%s\n",newUser.account,newUser.password);
	system("attrib +h .Useraccount.csv");
	fclose(fp);
}


